package com.duqc.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duqc.web.dao.MyBatisPlusMapper;
import com.duqc.web.entity.MyBatisPlus;
import com.duqc.web.service.MyBatisPlusService;
import com.duqc.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MyBatisPlusServiceImpl
 * @Description
 * @Author duqc
 * @Date 2021/7/29
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class MyBatisPlusServiceImpl implements MyBatisPlusService {

    private final MyBatisPlusMapper myBatisPlusMapper;

    @Override
    public List<MyBatisPlus> list() {
        return myBatisPlusMapper.selectList(null);
    }

    @Override
    public Integer insert(MyBatisPlus myBatisPlus) {
        myBatisPlus.setCreateTime(DateUtils.getNow());
        return myBatisPlusMapper.insert(myBatisPlus);
    }

    @Override
    public Integer update(MyBatisPlus myBatisPlus) {
        myBatisPlus.setUpdateTime(DateUtils.getNow());
        return myBatisPlusMapper.updateById(myBatisPlus);
    }

    @Override
    public Integer delete(String id) {
        return myBatisPlusMapper.deleteById(id);
    }

    @Override
    public List<MyBatisPlus> wrapperQuery() {
        //等于
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("user_id", "111");
//        List list = myBatisPlusMapper.selectList(queryWrapper);
//        return list;

        //like
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.like("user_id", "2");
//        List list = myBatisPlusMapper.selectList(queryWrapper);
//        return list;

        //小于是lt()
        //大于等于是ge()
        //小于等于是le()
        //范围的话，则使用between()
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.le("user_id", "111");
//        List list = myBatisPlusMapper.selectList(queryWrapper);
//        return list;

//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.isNotNull("user_id");
//        List list = myBatisPlusMapper.selectList(queryWrapper);
//        return list;

//        QueryWrapper queryWrapper = new QueryWrapper();
//        List<String> strings = new ArrayList<>();
//        strings.add("1");
//        strings.add("2");
//        queryWrapper.in("id", strings);
//        List list = myBatisPlusMapper.selectList(queryWrapper);
//        return list;

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("major_id", "222");
        queryWrapper.like("user_id", "1");
        queryWrapper.isNotNull("user_id");
        queryWrapper.orderByDesc("user_id");
        List list = myBatisPlusMapper.selectList(queryWrapper);
        return list;

//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.inSql("id","select id from user where age > 30");
//        return myBatisPlusMapper.selectList(queryWrapper);
    }

    @Override
    public int wrapperUpdate() {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", "5");
        updateWrapper.set("update_time", DateUtils.getNow());
        return myBatisPlusMapper.update(new MyBatisPlus(), updateWrapper);
    }

    @Override
    public List<MyBatisPlus> wrapperQueryLambda() {
        LambdaQueryWrapper<MyBatisPlus> lambdaQueryWrapper = new QueryWrapper<MyBatisPlus>().lambda();
        lambdaQueryWrapper.eq(MyBatisPlus::getUserId, "111");
        return myBatisPlusMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage<MyBatisPlus> listPage() {
        Page<MyBatisPlus> plusPage = new Page<>();
        plusPage.setCurrent(1L);
        plusPage.setSize(10L);
        return myBatisPlusMapper.listPage(plusPage, "1");
    }
}
